package com.example.qixin0314;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixin0314.bean.ObjectModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.LCUser;
import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CompanyActivity extends AppCompatActivity {
    ImageView company_search;
    ImageView image_love;
    String uri;
    String name;
    String puri;
    boolean type;
    LCUser currentUser;
    String id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company);
        initview();
        company();
        Aimage_love();

    }

    private void initview() {
        TextView text_view = findViewById(R.id.text_view);
        WebView webView = findViewById(R.id.myWebView);
        image_love = findViewById(R.id.image_love);

        Intent intent3 = getIntent();
        if (intent3.getExtras() != null) {
            uri = intent3.getExtras().getString("uri");
            name = intent3.getExtras().getString("name");
            puri = intent3.getExtras().getString("puri");
            text_view.setText(name);
            webView.loadUrl(uri);


            currentUser = LCUser.getCurrentUser();
            if (currentUser!=null){
                LCQuery<LCObject> query = new LCQuery<>("focus");
                query.whereEqualTo("id", currentUser.getObjectId());
                query.findInBackground()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<LCObject>>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {
                            }

                            @Override
                            public void onNext(@NotNull List<LCObject> lcObjects) {
                                for (int i = 0; i < lcObjects.size(); i++) {
                                    if (name.equals(lcObjects.get(i).get("title"))) {
                                        image_love.setImageResource(R.drawable.ic_baseline_favorite_24);
                                        type = true;
                                        id = lcObjects.get(i).getObjectId();
                                    }

                                }

                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        }

    }

    private void company() {
        company_search = findViewById(R.id.company_search);
        company_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CompanyActivity.this.finish();
            }
        });
    }

    private void Aimage_love() {
        image_love.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentUser != null) {
                    if (type){
                        LCObject todo=LCObject.createWithoutData("focus",id);
                        todo.deleteInBackground()
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<LCNull>() {
                                    @Override
                                    public void onSubscribe(@NotNull Disposable d) { }

                                    @Override
                                    public void onNext(@NotNull LCNull lcNull) {
                                        Toast.makeText(CompanyActivity.this,"已取消关注",Toast.LENGTH_SHORT).show();
                                        image_love.setImageResource(R.drawable.ic_baseline_favorite_border_24);
                                        type=false;
                                    }

                                    @Override
                                    public void onError(@NotNull Throwable e) {
                                        Toast.makeText(CompanyActivity.this,"取关失败",Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });

                    }else {
                        LCObject todo=new LCObject("focus");
                                            todo.put("title",name);
                                            todo.put("url",uri);
                                            todo.put("id",currentUser.getObjectId());
                                            todo.put("imageurl",puri);
                                            todo.saveInBackground()
                                                    .subscribeOn(Schedulers.io())
                                                    .observeOn(AndroidSchedulers.mainThread())
                                                    .subscribe(new Observer<LCObject>() {
                                                        @Override
                                                        public void onSubscribe(@NotNull Disposable d) { }

                                                        @Override
                                                        public void onNext(@NotNull LCObject lcObject) {
                                                            image_love.setImageResource(R.drawable.ic_baseline_favorite_24);
                                                            type=true;
                                                            Toast.makeText(CompanyActivity.this,"关注成功",Toast.LENGTH_SHORT).show();
                                                        }

                                                        @Override
                                                        public void onError(@NotNull Throwable e) {
                                                            Toast.makeText(CompanyActivity.this,"关注失败",Toast.LENGTH_SHORT).show();

                                                        }

                                                        @Override
                                                        public void onComplete() {

                                                        }
                                                    });
                    }

                }else {
                    Intent intent=new Intent(CompanyActivity.this,LoginActivity.class);
                    startActivity(intent);
                }

            }
        });
    }
}